package services;

import helpers.ConsoleUtils;

import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.apache.http.client.utils.URIBuilder;

import models.Project;
import models.Resource;
import models.ResourceType;
import models.Setting;

public class TestCaseService extends ResourceService {

	private final TestSuiteService testSuiteService = TestSuiteService.getService();
	private final SettingService settingService = SettingService.getService();

	private static TestCaseService service;

	// Singleton pattern
	private TestCaseService() {
	}

	public static TestCaseService getService() {
		if (service == null) {
			service = new TestCaseService();
		}
		return service;
	}

	public List<Resource> getAllTestCases(long testSuiteId) {
		List<Resource> resources = new ArrayList<Resource>();
		Resource testSuite = testSuiteService.getTestSuite(testSuiteId);
		if (testSuite != null) {
			resources.addAll(testSuite.getResources());

			for (int i = resources.size() - 1; i >= 0; i--) {
				if (resources.get(i).getType() != ResourceType.TestCase) {
					resources.remove(i);
				}
			}
		}
		return resources;
	}

	public Resource getTestCase(long testCaseId) {
		return Resource.find.byId(testCaseId);
	}

	public Resource getTestCase(Resource testSuite, String testCaseName) {
		Resource testCase = null;
		List<Resource> testCases = getAllTestCases(testSuite.getId());
		if (testCases != null && !testCases.isEmpty()) {
			for (Resource currentTestCase : testCases) {
				if (currentTestCase.getName().equalsIgnoreCase(testCaseName)) {
					testCase = currentTestCase;
					break;
				}
			}
		}
		return testCase;
	}

	public Resource saveTestCase(Resource testCase) {
		Resource result = testCase;
		if (testCase.getId() <= 0) {
			result = createTestCase(testCase);
		} else {
			result = updateTestCase(testCase);
		}
		return result;
	}

	private Resource createTestCase(Resource testCase) {
		Date currentDate = new Date();
		testCase.setType(ResourceType.TestCase);
		testCase.setCreatedDate(currentDate);
		testCase.setLastModifiedDate(currentDate);
		testCase.save();
		return testCase;
	}

	private Resource updateTestCase(Resource testCase) {
		testCase.setLastModifiedDate(new Date());
		testCase.save();
		return testCase;
	}

	public URL getTestExecuteURL(Project project, String testSuiteName, String testCaseName, Resource config) throws MalformedURLException, URISyntaxException {
		URIBuilder builder = new URIBuilder(settingService.getSetting(Setting.TEST_EXECUTION_URL).getStringValue());
		builder.addParameter("companyid", ConsoleUtils.getCompany().getCompanyId());
		builder.addParameter("projectid", Long.toString(project.getId()));
		builder.addParameter("suite", testSuiteName);
		builder.addParameter("tcase", testCaseName);
		builder.addParameter("variablefile", FilenameUtils.getName(config.getResourcePath()));
		return builder.build().toURL();
	}

}
